const sendMessage = (data) => {
  chrome?.runtime?.sendMessage(data, (response) => {
    // console.log('--收到回复--', response)
  })
}

window.addEventListener('message', (res) => {
  if (res.data?.key === 'chrome_plugin_demo') {
    // console.log('--content message--', res.data)
    sendMessage(res.data)
  }
})

// 测试
const injectScript = (jsPath = 'js/inject.js') => {
  const s = document.createElement('script')
  s.setAttribute('type', 'text/javascript')
  s.setAttribute('charset', 'UTF-8')
  s.src = chrome.runtime.getURL(jsPath)
  document.body.appendChild(s)
}
const injectFunc = () => {
  setTimeout(() => {
    if (document && document.body && document.body.appendChild) {
      injectScript()
    } else {
      injectFunc()
    }
  }, 100);
}
injectFunc()
